package com.glsc.ngateway.flowable.service.process;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.glsc.ngateway.common.base.domain.mysql.flowable.FlowableCommentSupplement;
import com.glsc.ngateway.common.base.repo.mysql.flowable.FlowableCommentSupplementRepo;
import com.glsc.ngateway.flowable.constant.FlowableConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class FlowableCommentSupplementService {

    Logger logger = LoggerFactory.getLogger(FlowableCommentSupplementService.class);

    @Autowired
    FlowableConstant flowableConstant;

    @Autowired
    FlowableCommentSupplementRepo flowableCommentSupplementRepo;


    public void save(FlowableCommentSupplement fcs) {
        flowableCommentSupplementRepo.save(fcs);
    }

    public List<FlowableCommentSupplement> findByTaskId(String taskId) {
        return flowableCommentSupplementRepo.findByTaskId(taskId);
    }

    public List<FlowableCommentSupplement> findByTaskIdList(List<String> collect) {
        return flowableCommentSupplementRepo.findByTaskIdList(collect);
    }

    public void signTask(String taskId) {
        this.signTask(taskId, null);
    }

    public void signTask(String taskId, String processId) {
        List<FlowableCommentSupplement> fcsList = this.findByTaskId(taskId);
        if (CollUtil.isNotEmpty(fcsList)) {
            fcsList.forEach(fcs -> {
                if (StrUtil.isNotBlank(processId) && StrUtil.isBlank(fcs.getProcessId())) {
                    fcs.setProcessId(processId);
                }
                fcs.setTaskHandleStatus(FlowableConstant.FLOWABLE_TASK_HANDLE_STATUS_SIGN);
            });
            this.saveAll(fcsList);
        }
    }

    public void saveAll(List<FlowableCommentSupplement> fcsList) {
        flowableCommentSupplementRepo.saveAll(fcsList);
    }
}
